<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

require_once(APPPATH .'interfaces/Cms_interface'. EXT);

class Super_admin extends CI_Controller {
	
	function __construct()
	{
		parent::__construct();
		
		$this->load->library('Json_formatter');
		
		$this->load->helper(array(
			'date',
			'file',
			'form',
			'html',
			'language',
		));
	}
	
	/**
	 * _upload_image
	 * 
	 * Process uploading of image. 
	 * Echo error message upon failure. Gives response to browser.
	 * 
	 * @access private
	 * @param string
	 * @param array
	 * @return boolean/string
	 */
	function _upload_image($field = 'image', $params = array())
	{
		$args = array(
			'image_dir' => NULL,
			'thumb' => FALSE,
			'thumb_dir' => NULL,
			'thumb_width' => NULL,
			'thumb_height' => NULL,
			'resize' => FALSE,
			'max_width' => NULL,
			'max_height' => NULL,
		);
		
		if (count($params) > 0)
		{
			foreach ($params as $i => $val)
			{
				$args[$i] = $val;
			}
		}
		else 
		{
			foreach ($args as $i => $val)
			{
				$args[$i] = $this->$i; 
			}
		}
		
		$this->load->library('Image_upload', array(
			'field' => $field,
			'upload_path' => $args['image_dir'],
			'encrypt_name' => TRUE,
			'thumb' => $args['thumb'],
			'thumb_path' => $args['thumb_dir'],
			'w_thumb' => $args['thumb_width'],
			'h_thumb' => $args['thumb_height'],
			'resize' => $args['resize'],
			'w_max' => $args['max_width'],
			'h_max' => $args['max_height'],
		), 'uploader');
		
		$image = $this->uploader->run();
		
		unset($this->uploader);
		
		if ( ! is_array($image))
		{
			echo json_encode($image);
			return FALSE;
		}
		
		return $image['file_name'];
	}
	
	/**
	 * _create_permalink
	 * 
	 * Create unique permalink. 
	 * Appends a numeric suffix for existing permalinks.
	 * 
	 * @access private
	 * @param string
	 * @param integer
	 * @param string
	 * @return string
	 */
	function _create_permalink($title, $id, $model)
	{
		$i = 0;
		do {
			$permalink = url_title($title, 'dash', TRUE);
			
			$permalink .= ($i == 0) ? NULL : $i;
			
			$where = array(
				'permalink' => $permalink,
			);
			
			if ( ! empty($id))
			{
				$primary_key = $this->$model->get_id();
				
				$where[$primary_key .' !='] = $id;
			}
		
			$exist = $this->$model->exists($where);
			
			$i++;
		} while ($exist == TRUE);
		
		return $permalink;
	}
        /**
	 * _is_authorize
	 *
	 * Check if user is authorize.
	 * Redirect if not.
	 *
	 * @access private
	 * @param array (e.g. user types)
	 * @return request
	 */
        function _is_authorize($access=array()) {
            $user_type = _session('user_type');

            if (empty($user_type)) redirect('home/unauthorized');

            foreach($access as $ac) {
                $has_access = false;

                if ($ac == _session('user_type')) {
                    $has_access = true;
                    break;
                }
            }

            if ($has_access === false) redirect('home/unauthorized');
        }
}
/* End of file super_admin.php */
/* Location: ./application/controllers/super_admin.php */
